package com.software.loginregister.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.software.loginregister.R;
import com.software.loginregister.entity.HealthFood;

import java.util.ArrayList;
import java.util.List;

public class HealthCardAdapter extends RecyclerView.Adapter<HealthCardAdapter.ViewHolder> {
    private List<HealthFood> healthFoodList = new ArrayList<>();

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_health_card, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HealthFood food = healthFoodList.get(position);
        holder.ivHealthCardFood.setImageResource(food.getImageResId());
        holder.tvHealthTitle.setText(food.getTitle());
        holder.tvHealthCardIngredients.setText(food.getIngredients());
        holder.tvViewCount.setText(String.format("%d 浏览", food.getViewCount()));
    }

    @Override
    public int getItemCount() {
        return healthFoodList.size();
    }

    public void setData(List<HealthFood> data) {
        this.healthFoodList = data;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivHealthCardFood;
        TextView tvHealthTitle;
        TextView tvHealthCardIngredients;
        TextView tvViewCount;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivHealthCardFood = itemView.findViewById(R.id.iv_health_card_food);
            tvHealthTitle = itemView.findViewById(R.id.tv_healthy_title);
            tvHealthCardIngredients = itemView.findViewById(R.id.tv_health_card_ingredients);
            tvViewCount = itemView.findViewById(R.id.tv_view_count);
        }
    }
}